package com.vhly.tuite.twclient.client;

import com.vhly.tuite.twclient.model.Status;
import com.vhly.tuite.twclient.util.QuickArrayInputStream;
import com.vhly.tuite.twclient.util.StreamUtil;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import java.io.IOException;
import java.util.Vector;

/**
 * Created by IntelliJ IDEA.
 * User: vhly[FR]
 * Date: 2010-6-16
 * Time: 22:46:54
 * Email: vhly@163.com
 */
public final class TwitterParser {

    private static DocumentBuilderFactory factory;

    private TwitterParser(){

    }


    public static Element parseXML(String xml){
        Element ret = null;
        if (xml != null) {
            if (factory == null) {
                factory = DocumentBuilderFactory.newInstance();
            }
            QuickArrayInputStream qin = null;
            try {
                DocumentBuilder builder = factory.newDocumentBuilder();
                qin = new QuickArrayInputStream(xml.getBytes("UTF-8"));
                Document doc = builder.parse(qin);
                ret = doc.getDocumentElement();
            } catch (Exception ex) {
                ex.printStackTrace();
            } finally {
                StreamUtil.close(qin);
            }
        }
        return ret;
    }


    public static Vector<Status> parseTweets(String xml) {
        Vector<Status> ret = null;
        if (xml != null) {
            if (factory == null) {
                factory = DocumentBuilderFactory.newInstance();
            }
            QuickArrayInputStream qin = null;
            try {
                DocumentBuilder builder = factory.newDocumentBuilder();
                qin = new QuickArrayInputStream(xml.getBytes("UTF-8"));
                Document doc = builder.parse(qin);
                Element el = doc.getDocumentElement();
                NodeList nl = el.getElementsByTagName("status");
                if (nl != null) {
                    int len = nl.getLength();
                    Element ee;
                    ret = new Vector<Status>();
                    Status st;
                    for (int i = 0; i < len; i++) {
                        ee = (Element) nl.item(i);
                        st = new Status();
                        st.parse(ee);
                        ret.add(st);
                    }
                }
            } catch (Exception ex) {
                ex.printStackTrace();
            } finally {
                StreamUtil.close(qin);
            }
        }
        return ret;
    }


    public static Vector<Status> parseTweets(Element el) {
        Vector<Status> ret = null;
        if (el != null) {
            try {
                NodeList nl = el.getElementsByTagName("status");
                if (nl != null) {
                    int len = nl.getLength();
                    Element ee;
                    ret = new Vector<Status>();
                    Status st;
                    for (int i = 0; i < len; i++) {
                        ee = (Element) nl.item(i);
                        st = new Status();
                        st.parse(ee);
                        ret.add(st);
                    }
                }
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
        return ret;
    }
}
